#!/usr/bin/ruby1.9

BYTEPERLINE = 16

def	dumpHexLine addr,line,count
	str = "%04X    " % addr
	if count > BYTEPERLINE
		count = BYTEPERLINE
	end

	i = 0
	line.each_byte do |c|
		if i < count
			str += "%02X " % c
		end
		i += 1
	end
	while i < BYTEPERLINE
		str += "   "
		i += 1
	end

	str += "   |"

	i = 0
	line.each_byte do |c|
		if i < count
			if c < 0x20 or c > 0x7F
				c = '.'
			end
			str += "%c" % c
		end
		i += 1
	end
	while i < BYTEPERLINE
		str += " "
		i += 1
	end

	str += "|\n"

	return str
end

def	dumpHex buf, len
	str = ""
	start = 0
	while start < len
		stop = start + BYTEPERLINE
		if stop > len
			stop = len
		end
		str += dumpHexLine start, buf[start,stop], BYTEPERLINE
		start = stop
	end
	return str
end

##### MAIN #####
if ARGV == []
	print "Usage: $0 file\n"
	exit(0)
end

buf = File.read(ARGV[0])
print dumpHex buf, buf.length()
